package com.mcxx.modules.minimumliving.controller;


import com.alibaba.fastjson.JSON;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.minimumliving.service.CalculateService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.family.service.read.FamilyMemberReadService;
import com.mcxx.modules.miniSalvation.family.service.read.FamilySupportReadService;
import com.mcxx.modules.miniSalvation.order.entity.IncomeCalculate;
import com.mcxx.modules.miniSalvation.order.entity.IncomeCert;
import com.mcxx.modules.miniSalvation.order.service.read.IncomeCalculateReadService;
import com.mcxx.modules.miniSalvation.order.service.read.IncomeCertReadService;
import com.mcxx.modules.miniSalvation.order.service.write.IncomeCalculateWriteService;
import com.mcxx.modules.miniSalvation.order.service.write.IncomeCertWriteService;
import com.mcxx.service.DefaultRestService;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;


@Slf4j
@Validated
@Api("精准计算类")
@RestService({"minimumliving/salvation/calculate"})
public class CalculateController extends DefaultRestService {
    @Autowired
    FamilyMemberReadService familyMemberReadService;
    @Autowired
    FamilySupportReadService familySupportReadService;
    @Autowired
    IncomeCalculateWriteService incomeCalculateWriteService;
    @Autowired
    IncomeCertReadService incomeCertReadService;
    @Autowired
    FileService fileService;
    @Autowired
    CalculateService calculateService;
    @Autowired
    IncomeCalculateReadService incomeCalculateReadService;
    @Autowired
    IncomeCertWriteService incomeCertWriteService;
    private static final Logger logger = LoggerFactory.getLogger(CalculateController.class);


    @PostMapping("getCalculateHistory")
    @ApiOperation("根据orderId显示order信息")
    public void getCalculateHistory(@V({"notnull"}) String orderId) {
        logger.info("根据orderId显示精准计算信息，参数是：{}", orderId);
        String content = incomeCalculateReadService.selectContentByOrderId(orderId);
        setData(content);
        logger.info("根据orderId显示order信息结束");
    }

    @PostMapping("saveCalculate")
    @ApiOperation("保存精准计算记录")
    public void savCalculate(IncomeCalculate entity) {
        logger.info("保存精准计算记录，参数是：{}", entity);
        int i = calculateService.savCalculate(entity,getCurUserAccount());
        setData(i);
        logger.info("保存精准计算记录结束");
    }


    @PostMapping("getMemberInfoByFamilyId")
    @ApiOperation("精准计算根据familyId查询成员和赡抚养人信息")
    public void getMemberInfoByFamilyId(@V({"notnull"}) String familyId,@V({"notnull"}) String orderId,@V({"businessType"}) String businessType) {
        logger.info("根据familyId查询成员和赡抚养人信息，参数是familyId:{},orderId:{},businessType:{}", familyId,orderId,businessType);
        Map<String,Object> resultMap = calculateService.getMemberInfoByFamilyId(familyId,orderId,businessType,getCurUserAccount());
        setData(resultMap);
        logger.info("精准计算根据familyId查询成员和赡抚养人信息结束");
    }


    @PostMapping("getCalculateCert")
    @ApiOperation("查询精准计算证明材料列表")
    public void getCalculateCert(String orderId,String memberId,String fileType){
        logger.info("查询精准计算证明材料列表，参数是{},{},{}",orderId,memberId,fileType);
        List<IncomeCert> certList = calculateService.getCalculateCert(orderId,memberId,fileType);
        setData(certList);
        logger.info("查询精准计算证明材料列表，结果是{}",JSON.toJSONString(certList));
    }

    @PostMapping("uploadCert")
    @ApiOperation(value="上传精准计算证明材料",notes="上传精准计算证明材料")
    public void upload(@RequestParam("file") MultipartFile file, IncomeCert incomeCert) {
        if(file.isEmpty()){
            throw new BizException("上传的文件是空的");
        }
        Map<String, Object> data = calculateService.uploadCert(file,incomeCert);
        setData(data);
        logger.info("证明材料上传成功，结果：{}",data);
    }


    @PostMapping("deleteIncomeCert")
    @ApiOperation(value="删除精准计算证明材料",notes="上传精准计算证明材料")
    public void deleteIncomeCert(String id) {
        logger.info("删除精准计算证明材料，参数是{}",id);
        int i = incomeCertWriteService.deleteById(id);
        setData(i);
        logger.info("证明材料上传成功，结果：{}",i);
    }

}
